create table "public"."agent_execution_logs" (
    "id" uuid not null default uuid_generate_v4(),
    "execution_id" uuid not null,
    "node_id" text,
    "step_number" integer,
    "log_type" character varying(50) not null,
    "content" jsonb not null,
    "timestamp" timestamp with time zone not null,
    "created_at" timestamp with time zone default now()
);


create table "public"."agent_executions" (
    "id" uuid not null default uuid_generate_v4(),
    "agent_id" uuid not null,
    "user_id" uuid,
    "trigger_id" uuid,
    "trigger_type" character varying(50) not null,
    "initial_context" jsonb,
    "state" character varying(50) not null,
    "final_outputs" jsonb,
    "error_message" text,
    "error_details" jsonb,
    "started_at" timestamp with time zone not null,
    "completed_at" timestamp with time zone,
    "created_at" timestamp with time zone default now(),
    "updated_at" timestamp with time zone default now()
);


create table "public"."user_agent_feedback" (
    "id" bigint generated by default as identity not null,
    "user_id" uuid default auth.uid(),
    "agent_id" uuid default auth.uid(),
    "created_at" timestamp with time zone not null default now(),
    "style" text,
    "feedback" integer
);


alter table "public"."user_agent_feedback" enable row level security;

CREATE UNIQUE INDEX agent_execution_logs_pkey ON public.agent_execution_logs USING btree (id);

CREATE UNIQUE INDEX agent_executions_pkey ON public.agent_executions USING btree (id);

CREATE INDEX idx_agent_execution_logs_execution_id ON public.agent_execution_logs USING btree (execution_id);

CREATE INDEX idx_agent_execution_logs_log_type ON public.agent_execution_logs USING btree (log_type);

CREATE INDEX idx_agent_execution_logs_timestamp ON public.agent_execution_logs USING btree ("timestamp");

CREATE INDEX idx_agent_executions_agent_id ON public.agent_executions USING btree (agent_id);

CREATE INDEX idx_agent_executions_started_at ON public.agent_executions USING btree (started_at);

CREATE INDEX idx_agent_executions_state ON public.agent_executions USING btree (state);

CREATE INDEX idx_agent_executions_user_id ON public.agent_executions USING btree (user_id);

CREATE UNIQUE INDEX user_agent_feedback_pkey ON public.user_agent_feedback USING btree (id);

alter table "public"."agent_execution_logs" add constraint "agent_execution_logs_pkey" PRIMARY KEY using index "agent_execution_logs_pkey";

alter table "public"."agent_executions" add constraint "agent_executions_pkey" PRIMARY KEY using index "agent_executions_pkey";

alter table "public"."user_agent_feedback" add constraint "user_agent_feedback_pkey" PRIMARY KEY using index "user_agent_feedback_pkey";

alter table "public"."agent_execution_logs" add constraint "agent_execution_logs_execution_id_fkey" FOREIGN KEY (execution_id) REFERENCES agent_executions(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;

alter table "public"."agent_execution_logs" validate constraint "agent_execution_logs_execution_id_fkey";

alter table "public"."agent_executions" add constraint "agent_executions_agent_id_fkey" FOREIGN KEY (agent_id) REFERENCES agents(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;

alter table "public"."agent_executions" validate constraint "agent_executions_agent_id_fkey";

alter table "public"."agent_executions" add constraint "agent_executions_trigger_id_fkey" FOREIGN KEY (trigger_id) REFERENCES agent_triggers(id) ON UPDATE CASCADE ON DELETE SET NULL not valid;

alter table "public"."agent_executions" validate constraint "agent_executions_trigger_id_fkey";

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.insert_auth_user_to_public()
 RETURNS trigger
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$
BEGIN
  INSERT INTO public."users" (id, email, created_at, updated_at)
  VALUES (
    NEW.id,
    NEW.email,
    NOW(),
    NOW()
  )
  ON CONFLICT (id) DO NOTHING;

  RETURN NEW;
END;
$function$
;

grant delete on table "public"."agent_execution_logs" to "anon";

grant insert on table "public"."agent_execution_logs" to "anon";

grant references on table "public"."agent_execution_logs" to "anon";

grant select on table "public"."agent_execution_logs" to "anon";

grant trigger on table "public"."agent_execution_logs" to "anon";

grant truncate on table "public"."agent_execution_logs" to "anon";

grant update on table "public"."agent_execution_logs" to "anon";

grant delete on table "public"."agent_execution_logs" to "authenticated";

grant insert on table "public"."agent_execution_logs" to "authenticated";

grant references on table "public"."agent_execution_logs" to "authenticated";

grant select on table "public"."agent_execution_logs" to "authenticated";

grant trigger on table "public"."agent_execution_logs" to "authenticated";

grant truncate on table "public"."agent_execution_logs" to "authenticated";

grant update on table "public"."agent_execution_logs" to "authenticated";

grant delete on table "public"."agent_execution_logs" to "service_role";

grant insert on table "public"."agent_execution_logs" to "service_role";

grant references on table "public"."agent_execution_logs" to "service_role";

grant select on table "public"."agent_execution_logs" to "service_role";

grant trigger on table "public"."agent_execution_logs" to "service_role";

grant truncate on table "public"."agent_execution_logs" to "service_role";

grant update on table "public"."agent_execution_logs" to "service_role";

grant delete on table "public"."agent_executions" to "anon";

grant insert on table "public"."agent_executions" to "anon";

grant references on table "public"."agent_executions" to "anon";

grant select on table "public"."agent_executions" to "anon";

grant trigger on table "public"."agent_executions" to "anon";

grant truncate on table "public"."agent_executions" to "anon";

grant update on table "public"."agent_executions" to "anon";

grant delete on table "public"."agent_executions" to "authenticated";

grant insert on table "public"."agent_executions" to "authenticated";

grant references on table "public"."agent_executions" to "authenticated";

grant select on table "public"."agent_executions" to "authenticated";

grant trigger on table "public"."agent_executions" to "authenticated";

grant truncate on table "public"."agent_executions" to "authenticated";

grant update on table "public"."agent_executions" to "authenticated";

grant delete on table "public"."agent_executions" to "service_role";

grant insert on table "public"."agent_executions" to "service_role";

grant references on table "public"."agent_executions" to "service_role";

grant select on table "public"."agent_executions" to "service_role";

grant trigger on table "public"."agent_executions" to "service_role";

grant truncate on table "public"."agent_executions" to "service_role";

grant update on table "public"."agent_executions" to "service_role";

grant delete on table "public"."user_agent_feedback" to "anon";

grant insert on table "public"."user_agent_feedback" to "anon";

grant references on table "public"."user_agent_feedback" to "anon";

grant select on table "public"."user_agent_feedback" to "anon";

grant trigger on table "public"."user_agent_feedback" to "anon";

grant truncate on table "public"."user_agent_feedback" to "anon";

grant update on table "public"."user_agent_feedback" to "anon";

grant delete on table "public"."user_agent_feedback" to "authenticated";

grant insert on table "public"."user_agent_feedback" to "authenticated";

grant references on table "public"."user_agent_feedback" to "authenticated";

grant select on table "public"."user_agent_feedback" to "authenticated";

grant trigger on table "public"."user_agent_feedback" to "authenticated";

grant truncate on table "public"."user_agent_feedback" to "authenticated";

grant update on table "public"."user_agent_feedback" to "authenticated";

grant delete on table "public"."user_agent_feedback" to "service_role";

grant insert on table "public"."user_agent_feedback" to "service_role";

grant references on table "public"."user_agent_feedback" to "service_role";

grant select on table "public"."user_agent_feedback" to "service_role";

grant trigger on table "public"."user_agent_feedback" to "service_role";

grant truncate on table "public"."user_agent_feedback" to "service_role";

grant update on table "public"."user_agent_feedback" to "service_role";

create policy "allow authenticated users to insert feedback"
on "public"."user_agent_feedback"
as permissive
for insert
to public
with check ((auth.uid() = user_id));



